//
//  MKJCollectionViewCell.m
//  wangzhe
//
//  Created by 李 佳伟 on 2018/2/7.
//  Copyright © 2018年 李 佳伟. All rights reserved.
//

#import "MKJCollectionViewCell.h"

static CGFloat viewspace = 0;

@implementation MKJCollectionViewCell
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}



- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        width = frame.size.width;
        height =frame.size.height;
        backview = [[UIImageView alloc]initWithFrame:CGRectMake(viewspace, 0,width , height)];
        backview.userInteractionEnabled = YES;
        backview.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:backview];
        
        logoImageview = [[UIImageView alloc]initWithFrame:CGRectMake(21, 10,30 , 30)];
        logoImageview.userInteractionEnabled = YES;
        logoImageview.contentMode = UIViewContentModeScaleAspectFit;
        [backview addSubview:logoImageview];
        
        nameLabel = [UILabel initWithFrame:CGRectMake(KVIEW_X(logoImageview)+10, 15, width-(KVIEW_X(logoImageview)-20), 20) text:@"中国银行" font:14 textAlignment:NSTextAlignmentLeft texcolor:[UIColor whiteColor] backgroundColor:nil];
        [backview addSubview:nameLabel];
        
        numberLabel = [UILabel initWithFrame:CGRectMake(0, height/2-30, width, 30) text:@"6222 **** **** 4875" font:21 textAlignment:NSTextAlignmentCenter texcolor:[UIColor whiteColor] backgroundColor:nil];
        [backview addSubview:numberLabel];
        [numberLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        
        
        CGRect rect = [df labelHeightWithContent:@"还款" width:width font:12];
        CGFloat changetop = (height/2-10-rect.size.height*2)/2;
        huankuanLabel = [UILabel initWithFrame:CGRectMake(20, height/2+changetop, width-120, rect.size.height) text:@"还款日期" font:12 textAlignment:NSTextAlignmentLeft texcolor:[UIColor whiteColor] backgroundColor:nil];
        [backview addSubview:huankuanLabel];
        
        juliLabel = [UILabel initWithFrame:CGRectMake(20, KVIEW_Y(huankuanLabel)+10, width-120, rect.size.height) text:@"距离还款日期" font:12 textAlignment:NSTextAlignmentLeft texcolor:[UIColor whiteColor] backgroundColor:nil];
        [backview addSubview:juliLabel];
        
       
        
        
        
    }
    
    return self;
}

-(NSString *)getNowTimeTimestamp2:(NSString *)data{
    
    NSDate* dat;
    if (data == nil) {
        dat = [NSDate dateWithTimeIntervalSinceNow:0];
    }else{
        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        format.dateFormat = @"YYYY-MM-dd";
        dat = [format dateFromString:data];
    }
    NSTimeInterval a=[dat timeIntervalSince1970];
    NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
    return timeString ;
    
}
- (NSInteger)getdayWithTime:(NSString *)time{
    NSTimeInterval newtime=[time doubleValue];
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:newtime];
    NSLog(@"date:%@",[detaildate description]);
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"dd"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    
    return [currentDateStr integerValue];
}
- (NSInteger)getMothWithTime:(NSString *)time{
    NSTimeInterval newtime=[time doubleValue];
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:newtime];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"MM"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    
    return [currentDateStr integerValue];
}

- (NSInteger)getYearWithTime:(NSString *)time{
    NSTimeInterval newtime=[time doubleValue]+28800;
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:newtime];
    NSLog(@"date:%@",[detaildate description]);
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"YYYY"];
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return [currentDateStr integerValue];
}


- (void)setvalueWithModel:(MyBankCardModel *)model indexPath:(NSIndexPath*)index{
  
    backview.image = [UIImage imageNamed:[NSString stringWithFormat:@"bank%ld.png",index.row%3]];
    [df layerWithView:backview radius:4 width:0 color:nil];
    [df setTheViewYiYing:backview shadowColor:[UIColor blueColor] shadowOffset:CGSizeMake(3, 3) apla:0.5];
    
    [logoImageview sd_setImageWithURL:[NSURL URLWithString:model.bank_logo]];
    nameLabel.text = [NSString stringWithFormat:@"%@",model.bank_name];
    NSString *cardNum = [model.bank_card stringByReplacingCharactersInRange:NSMakeRange(4, 8) withString:@" **** **** "];
    numberLabel.text = [NSString stringWithFormat:@"%@",cardNum];
    
    huankuanLabel.text = [NSString stringWithFormat:@"还款日：%@",model.bill_date];
    
    NSString *nowTime = [self getNowTimeTimestamp2:nil] ;
    NSInteger year = [self getYearWithTime:nowTime];
    NSInteger moth = [self getMothWithTime:nowTime];
    NSInteger day  = [self getdayWithTime:nowTime];
    
    if (day>[model.bill_date integerValue]) {
        moth = moth+1;
        if (moth>12) {
            moth = 1;
            year = year+1;
        }
    }
    NSString *gethuankuanriqi = [NSString stringWithFormat:@"%ld-%ld-%@",year,moth,model.bill_date];
    NSString *huankuanTime = [self getNowTimeTimestamp2:gethuankuanriqi];
    juliLabel.text = [NSString stringWithFormat:@"距离还款日还剩%d天",abs(([nowTime intValue]-[huankuanTime intValue]))/86400];
    
}

@end
